﻿package org.display.graphic
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.*;
	import flash.utils.*;
	
	import mx.events.*;
	
	import org.abcFrame.core.*;
	import org.data.*;
	import org.display.graphic.*;
	import org.display.util.*;
	import org.enmu.*;
	import org.net.*;
	import org.utils.*;

	public class Iv extends ShapeBase
	{
		
		
		public var path:String=""
		public var data:XML
		public var viewdata:String=""
		public var modeldata:String=""
		
		public var maskbg:Sprite
		
		private var container:Sprite
		
		private var instance:Iv
		
		public var w:Number=0
		public var h:Number=0
			
		private var shapeArea:ShapeArea
		
		private var point:Point
			
		public function Iv(path:String)
		{
			super()
			
			createMenuItems(["重命名","复制","编辑","最上层","最下层","删除"])
			
			container=new Sprite()
			addChild(container)
			maskbg=new Sprite()
			this.path=path
			instance=this
				
			loadxml(path)
			
		    shapeArea=new ShapeArea(this)
			shapeArea.createMenuItems(["发布对象类","左端对齐","顶端对齐","竖向居中对齐","横向居中对齐"])	
				
		}

		function loadxml(path:String){
		
			//加载view的as模板文件========================================================
			var xmlRes:AbcResponder=new AbcResponder()
			xmlRes.onResult=function(e,path){
				
				data=XML(e)
				loadView()
				//loadModel()
				
				loadViewProxy()
				init()
				
				gotoroot(instance)
				
			}
				
			new AbcHTTPServices(path,xmlRes)
		
		}
		
		function gotoroot(obj){
		
			maskbg.graphics.clear()
			var rec:Rectangle=getBounds(parent)
			redraw(rec.width,rec.height)

			if(obj.parent is Iv){
				obj.parent.maskbg.graphics.clear()
			   var rec:Rectangle=obj.parent.getBounds(obj.parent.parent)
			   obj.parent.redraw(rec.width,rec.height)
			   gotoroot(obj.parent)
			}
		   
		}
		
		protected  function loadViewProxy(){
		
		
		
		
		}
		
		protected function loadView(){
			
			var xmlRes:AbcResponder=new AbcResponder()
			xmlRes.onResult=function(e,path){
				
				viewdata=e
			}
			var path:String=(Global.workspace+"/"+String(data.view)).replace(/\./gis,"/").replace(/\\/gis,"\/")+".as"

			new AbcHTTPServices(path,xmlRes)		
		}
		/*
		function loadModel(){
			
			var xmlRes:AbcResponder=new AbcResponder()
			xmlRes.onResult=function(e,path){
				
				modeldata=e
			}
			
			var path:String=(Global.workspace+"/"+String(data.model)).replace(/\./gis,"/").replace(/\\/gis,"\/")+".as"

			new AbcHTTPServices(path,xmlRes)		
			
		}*/
		
		public function init(){			
			
			var symbol:ShapeBase
			var ivs:Array=[]
			
			for(var item in data.item){
				
				var value=data.item[item]
				
				var ivname:String =value.name
				
				var arr:Array=ivname.split("_")
				
				switch(arr[arr.length-1]){
					
					case AbcType.BT:
						symbol=new Bt()
						break
					case AbcType.TF:
						symbol=new Tf()
						break
					case AbcType.MC:
						symbol=new Mc()
						break
					
					case AbcType.MBT:
						symbol=new Mbt()
						break
					
				}
				
				if(arr[arr.length-1]!=AbcType.IV){
				
					if(value.event=="true"){
					   symbol.addChild(symbol.square)
				    }

				    symbol.redraw(value.w,value.h)	 
				    symbol.setName(arr[0])
				   	
					
				}else{
				    
					symbol=new Iv((Global.workspace+value.path).replace(/\\/gis,"\/"))
					var arrcopy:Array=arr.concat()
					arrcopy.shift()
					arrcopy.pop()
					//trace(arrcopy.join("_")+":"+ivname)
					symbol.setName(arrcopy.join("_"))
						
				}
				
				symbol.x=value.x
				symbol.y=value.y
				addChild(symbol)

			}
		}
		
		public override function redraw(w:Number,h:Number){

			with(maskbg){
				graphics.clear()
				graphics.lineStyle(1,0x000000,0)
				graphics.beginFill(0xcccccc,0.8)
				graphics.drawRect(0,0,w,h)
				graphics.endFill()
			}
			
			addChild(maskbg)			
			
			hitRec.x=0
			hitRec.y=0
			hitRec.visible=false
			
			ElementControl.cleanObj(hitRec)
		}
		
		public function setObjs(scene){
		
			var objs:Array=ElementControl.getObjs(ShapeBase,this)
			for(var k=0;k<objs.length;k++){
				scene.addChild(objs[k])
			}
			
		}
		
		public function getModelEvents():Array{

			var reg:RegExp=/(?<=public function get ).*?(?=\(\))/gis
			var events:Array=modeldata.match(reg)
			
			return events
			
		}
		
		public function getClassSpace():String{
			
			return String(data.view)
			
		}
		
		public function getViewName():String{
			
			var name:String=""
			try{ name= String(data.view).split(".")[String(data.view).split(".").length-1];}
			catch(e:Error){
			    trace(path)
			}
			return name
			
		}
		
		/*
		public function getModelName():String{
			
			var name:String="";
			
			try{ 
				name = String(data.model).split(".")[String(data.model).split(".").length-1];
			
			}catch(e:Error){
				
			}
			
			return name
			
		}*/
		
		public function getType():String{
			
			return String(data.type)
			
		}
		
		public override function getName():String{
			
			if(txtField.text==""){return ""}
			return getViewName()+"_"+txtField.text+"_iv"
			
		}
		
		protected override function clone():ShapeBase{
			
			var copy:Class=getDefinitionByName(getQualifiedClassName(this).replace(/::/gis,".")) as Class
			var symbol:Iv=new copy(path) as Iv
			var rec:Rectangle=getBounds(parent)
			//symbol.redraw(rec.width,rec.height)
			return symbol
			
		}
		
		protected override function menuselectEvent(e:ContextMenuEvent){

			super.menuselectEvent(e)
				
			switch(ContextMenuItem(e.currentTarget).caption){
				
				case "编辑":	
					
					Global.editObj=this
					
					parent.addChild(this)
					
					var rec:Rectangle=this.getBounds(parent)
					maskbg.x=-rec.x
					maskbg.y=-rec.y
					
					redraw(Global.sceneSize,Global.sceneSize)
					this.setChildIndex(maskbg,0)
					
					createEditMenuItems()
					
					maskbg.addEventListener(MouseEvent.MOUSE_DOWN,maskmouseEvent)
					maskbg.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN,maskmouseEvent)
					
					this.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
					
					this.removeEventListener(MouseEvent.DOUBLE_CLICK,mouseEvent)
					
					break
				
			}

		}
		
		public function getIvs():Array{
		
		var ivs:Array=[]
		for(var item in data.item){
		
		var name:String=data.item[item].name
		if(name.split("_")[name.split("_").length-1]=="iv"){
		
		ivs.push(name)
		
		}
		}
		
		return ivs
		
		}
		
		function createEditMenuItems(){
			
			var myContextMenu:ContextMenu = new ContextMenu();
			myContextMenu.hideBuiltInItems();
			
			createItem("保存")
			createItem("添加<bt>")
			createItem("添加<mc>")
			createItem("添加<tf>")
			createItem("添加<mbt>")
			createItem("另存为当前锁定路径")
			createItem("返回")
			
			this.contextMenu = null;
			maskbg.contextMenu=myContextMenu
			
			function createItem(vlaue:String){
				
				var item:ContextMenuItem = new ContextMenuItem(vlaue);
				item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT ,menuEditMenuItems)
				myContextMenu.customItems.push(item);
				
			}
			
		}
		
		
		function menuEditMenuItems(e:ContextMenuEvent){
		
		
			switch(ContextMenuItem(e.currentTarget).caption){
				
				case "返回":
					Global.editObj= parent as Sprite
					
					var iv:Iv=new Iv(path)
					Global.editObj.addChild(iv)
					
					iv.x=x
					iv.y=y
					
					ElementControl.cleanObj(this)
					
					break
				
				
				case "保存":
					
					Global.editObj= parent as Sprite
					
					//file:///D:/abcFF/xml/org/display/aishi/TestView.xml
					
					path=path.replace(/\\/gis,"/")
					
					var arr:Array=path.replace(/.*?(\/xml\/)/gis,"").split("\/")
					arr.pop()
					var lockPath:String="\\"+arr.join("\\")
					
					
					var lockPathCopy:String=Global.lockPath
					Global.lockPath=lockPath
					
					new CreateClass(this,String(data.name),String(data.type),Global.lockPath)
					
					Global.lockPath=lockPathCopy
					
					maskbg.graphics.clear()
					
				    ElementControl.cleanObj(maskbg)
					
					var rec:Rectangle=this.getBounds(parent)
					redraw(rec.width,rec.height)
					
					//重设内部元素坐标==========================================
					maskbg.x=0
					maskbg.y=0
					
					var objs:Array=ElementControl.getObjs(ShapeBase,this)
					
					for(var i=0;i<objs.length;i++){
						objs[i].x-=rec.x-x
						objs[i].y-=rec.y-y
					}
					//重设内部元素坐标完毕======================================

					this.setChildIndex(maskbg,this.numChildren-1)
					
					maskbg.contextMenu=null
					
					this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
					
					this.addEventListener(MouseEvent.DOUBLE_CLICK,mouseEvent)
					
					createMenuItems(["重命名","复制","编辑","最上层","最下层","删除"])
					
					break
				
				case "另存为当前锁定路径":
					
					Global.editObj= parent as Sprite
					
					//file:///D:/abcFF/xml/org/display/aishi/TestView.xml
					
					new CreateClass(this,String(data.name),String(data.type),Global.lockPath)
					
					maskbg.graphics.clear()
					
					ElementControl.cleanObj(maskbg)
					
					var rec:Rectangle=this.getBounds(parent)
					redraw(rec.width,rec.height)
					
					//重设内部元素坐标==========================================
					maskbg.x=0
					maskbg.y=0
					
					var objs:Array=ElementControl.getObjs(ShapeBase,this)
					
					for(var i=0;i<objs.length;i++){
						objs[i].x-=rec.x-x
						objs[i].y-=rec.y-y
					}
					//重设内部元素坐标完毕======================================
					
					this.setChildIndex(maskbg,this.numChildren-1)
					
					maskbg.contextMenu=null
					
					this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
					
					this.addEventListener(MouseEvent.DOUBLE_CLICK,mouseEvent)
					
					createMenuItems(["重命名","复制","编辑","最上层","最下层","删除"])
					
					break
				
				
				case "添加<bt>":
					var shape=new Bt()
					shape.x=mouseX
					shape.y=mouseY
					addChild(shape)
					break
				
				case "添加<mc>":
					shape=new Mc()
					shape.x=mouseX
					shape.y=mouseY
					addChild(shape)
					
					break
				
				case "添加<tf>":
					shape=new Tf()
					shape.x=mouseX
					shape.y=mouseY
					addChild(shape)
					
					break
				
				case "添加<mbt>":
					
					shape=new Mbt()
					shape.x=mouseX
					shape.y=mouseY
					addChild(shape)
					
					break
				
			}
		
		
		
		}
		
		
		function maskmouseEvent(e:MouseEvent){
			
			
			switch(e.type){
				
				case MouseEvent.MOUSE_DOWN:
					
					shapeArea.mousedownEvent()
					
					this.addChild(shapeArea)
					
					point=new Point(mouseX,mouseY)

					shapeArea.x=point.x
					shapeArea.y=point.y
					
					stage.addEventListener(MouseEvent.MOUSE_MOVE,maskmouseEvent)
					stage.addEventListener(MouseEvent.MOUSE_UP,maskmouseEvent)
					
					break
				
				case MouseEvent.MOUSE_MOVE:
					
					shapeArea.redraw(mouseX-point.x,mouseY-point.y)
					
					break
				
				case MouseEvent.MOUSE_UP:
					
					shapeArea.mouseupEvent()
					
					stage.removeEventListener(MouseEvent.MOUSE_MOVE,maskmouseEvent)
					stage.removeEventListener(MouseEvent.MOUSE_UP,maskmouseEvent)
					break
				
				case MouseEvent.RIGHT_MOUSE_DOWN:
					
					shapeArea.mousedownEvent()
					
					break
				

			}
			

		}
		
		//重新保存
		public function resave(){
		
			var arr:Array=path.replace(/.*?\/xml\//gis,"").split("\/")
			arr.pop()
			var lockPath:String="\\"+arr.join("\\")
			
			var lockPathCopy:String=Global.lockPath
			Global.lockPath=lockPath
			
			new CreateClass(this,String(data.name),String(data.type),Global.lockPath)
			
			Global.lockPath=lockPathCopy
		
		}
		
		
	}
}



